package framework.multimedia;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Lukáš Marek
 */
public class FRDMultimediaItem extends JPanel
{
	protected BufferedImage image = null;
	protected int id = 0;
	public JLabel name;
	
	public FRDMultimediaItem()
	{
		setBounds(0, 0, 240, 210);
		this.setLayout(null);
		
		name = new JLabel("", JLabel.CENTER);
		name.setBounds(0, 0, 240, 20);
		this.add(name);
		
		JButton detailButton = new JButton();
		detailButton.setText("detail");
		detailButton.setBounds(160, 190, 80, 20);
		detailButton.setActionCommand("detailButton");
		this.add(detailButton);
	}
	
	/**
	 * Nastaveni listeneru vsem prvkum
	 * @param listener 
	 */
	@Override
	public void addMouseListener(MouseListener listener)
	{
		super.addMouseListener(listener);
		for(Component component: this.getComponents())
		{
			if (component instanceof JButton)
				((JButton)component).addActionListener((ActionListener) listener);
			else
				component.addMouseListener(listener);
		}
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	/**
	 * Vykresleni obrazku
	 * @param g 
	 */
	@Override
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if (image != null)
		{
			g.drawImage(image, 0, 0, this);
		}
	}

	public BufferedImage getImage()
	{
		return image;
	}

	public void setImage(BufferedImage image)
	{
		this.image = image;
		if (image == null)
		{
			for(Component component: this.getComponents())
			{
				component.setVisible(false);
			}
		}
		else
		{
			for(Component component: this.getComponents())
			{
				component.setVisible(true);
			}
		}
	}
}
